using NL;
using NL.Advanced;
using System;

public static class DateTimeTools
{
    public static string TimeFormat(this TimeSpan time)
    {
        var result = "";
        if (time.TotalDays > 1)
        {
            //result += $"{time.Days}天 {time.ToString("hh\\:mm\\:ss")}";
            result += $"{time.ToString(_FormatIni["daytime"])}";
        }
        else
        {
            //result += $"{time.ToString("hh\\:mm\\:ss")}";
            result += $"{time.ToString(_FormatIni["hhmmss"])}";
        }

        return result;
    }

    public static string FormatTimeSpan(long seconds)
    {
        return FormatTimeSpan(TimeSpan.FromSeconds(seconds));
    }

    public static string FormatTimeSpanAllDayTime(long seconds)
    {
        return FormatTimeSpanAllDayTime(TimeSpan.FromSeconds(seconds));
    }

    /// <summary>
    /// 计算机元年
    /// </summary>
    public static DateTime UNIX_TIME = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

    public static long ToTimeSpanSeconds(this DateTime date)
    {
        if (date != null)
        {
            return (long)(date - UNIX_TIME).TotalSeconds;
        }
        return 0;
    }

    static NLIni _FormatIni
    {
        get
        {
            NLIni ini = new NLIni();
            string format_ss = "ss's'";
            string format_mmss = "mm'm 'ss's'";
            string format_mm = "mm'm'";
            string format_hhmm = "hh'h 'mm'm'";
            string format_hh = "hh'h'";
            string format_ddhh = "d'd 'hh'h'";
            string format_dd = "d'd";
            string format_hhmmss = "hh'h 'mm'm 'ss's'";
            string format_daytime = "d'd 'hh'h 'mm'm 'ss's'";

            if (Language.NOW != null)
            {
                format_ss = Language.NOW.Get("DURATION_SS");
                format_mmss = Language.NOW.Get("DURATION_MMSS");
                format_mm = Language.NOW.Get("DURATION_MM");
                format_hhmm = Language.NOW.Get("DURATION_HHMM");
                format_hh = Language.NOW.Get("DURATION_HH");
                format_ddhh = Language.NOW.Get("DURATION_DDHH");
                format_dd = Language.NOW.Get("DURATION_DD");
                format_hhmmss = Language.NOW.Get("DURATION_MHS");
                format_daytime = Language.NOW.Get("DURATION_DMHS");
            }

            ini["ss"] = format_ss;
            ini["mmss"] = format_mmss;
            ini["mm"] = format_mm;
            ini["hhmm"] = format_hhmm;
            ini["hh"] = format_hh;
            ini["ddhh"] = format_ddhh;
            ini["dd"] = format_dd;
            ini["hhmmss"] = format_hhmmss;
            ini["daytime"] = format_daytime;

            return ini;
        }
    }

    public static void FormatTest()
    {
        // TimeSpan的参考使用方法
        DateTime now = DateTime.Now;
        DateTime then = DateTime.Now.AddHours(-2).AddMinutes(-30);

        TimeSpan ts = now - then;

        string format = @"dd\:hh\:mm\:ss\.fffffff";
        Console.WriteLine("The time difference is: {0}", ts.ToString(format));

        Console.WriteLine(@"{0} Days, {1} Hours, {2} Minutes, {3} Seconds
                            and {4} Milliseconds", ts.Days, ts.Hours,
                            ts.Minutes, ts.Seconds, ts.Milliseconds);

        // 12 days, 23 hours, 24 minutes, 2 seconds.
        TimeSpan span = new TimeSpan(12, 23, 24, 2);
        // 27 hours, 24 minutes, 2 seconds
        TimeSpan span2 = new TimeSpan(27, 24, 2);


        string format0 = span.ToString("h'h 'm'm 's's'");        //23h 24m 2s
        //string format1 = span.ToString("H'时'M'分'S'秒'");        //23h 24m 2s
        string format1_1 = span.ToString("h'时'm'分's'秒'");        //23h 24m 2s
        string format2 = span2.ToString("h'h 'm'm 's's'");      //3h 24m 2s
        string format3 = span.ToString("d'd 'h'h 'm'm 's's'");  //12d 23h 24m 2s
        string format4 = span.ToString("hh' 'mm' 'ss");             //23 24 02
        string format5 = span.ToString("hh':'mm':'ss");         //23:24:02

        string.Format("{0}h{1}m{2}s", span.Hours, span.Minutes, span.Seconds);
    }

    /// <summary>
    /// 时间间隔格式化
    /// <code>
    /// 小于1分钟：显示：ss秒
    /// 小于1小时：显示：mm分ss秒
    /// 小于6小时：显示：hh时mm分
    /// 小于24小时：显示：hh时
    /// 小于144小时（6天）：显示：dd天hh时
    /// 否则：显示：dd天
    /// </code>
    /// </summary>
    /// <param name="timespan"></param>
    /// <returns></returns>
    public static string FormatTimeSpan(TimeSpan timespan)
    {
        double totalHours = timespan.TotalHours;//这个带小数。。。。
        if (totalHours < 0)
        {
            return "error";
        }
        else
        {
            NLIni formatIni = _FormatIni;
            if (totalHours < 1)
            {
                if (timespan.TotalMinutes < 1)
                {
                    return timespan.ToString(formatIni["ss"]);//("ss's'");
                }
                else
                {
                    //return timespan.ToString("m'm 's's'");
                    return timespan.ToString(formatIni["mmss"]);//("mm'm 'ss's'");
                }
            }
            else if (totalHours < 6)
            {
                //return timespan.ToString("h'h 'm'm'");
                return timespan.ToString(formatIni["hhmm"]);//("hh'h 'mm'm'");
            }
            else if (totalHours < 24)
            {
                //return timespan.ToString("h'h'");
                return timespan.ToString(formatIni["hh"]);//("hh'h'");
            }
            else if (totalHours < 144)
            {
                //return timespan.ToString("d'd 'h'h'");
                return timespan.ToString(formatIni["ddhh"]);//("d'd 'hh'h'");
            }
            else
            {
                return timespan.ToString(formatIni["dd"]);//("d'd'");
            }
        }
    }
    /// <summary>
    /// 显示x天 xx时xx分xx秒
    /// </summary>
    /// <param name="timespan"></param>
    /// <returns></returns>
    public static string FormatTimeSpanAllDayTime(TimeSpan timespan)
    {
        double totalHours = timespan.TotalHours;//这个带小数。。。。
        if (totalHours < 0)
        {
            return "error";
        }
        else
        {
            NLIni formatIni = _FormatIni;
            if (totalHours > 24)
            {
                if (formatIni.ContainsKey("daytime"))
                    return timespan.ToString(formatIni["daytime"]);
                else
                    return timespan.ToString();
            }
            else
            {
                if (formatIni.ContainsKey("hhmmss"))
                    return timespan.ToString(formatIni["hhmmss"]);
                else
                    return timespan.ToString();
            }
        }
    }

    /// <summary>
    /// 简短的时间间隔表示
    /// </summary>
    /// <param name="timespan"></param>
    /// <returns></returns>
    public static string ShortTimeSpan(TimeSpan timespan)
    {
        double totalHours = timespan.TotalHours;//这个带小数。。。。
        if (totalHours < 0)
        {
            return "";
        }
        else
        {
            NLIni formatIni = _FormatIni;
            if (totalHours < 1)
            {
                if (timespan.TotalMinutes < 1)
                {
                    return timespan.ToString(formatIni["ss"]);//("ss's'");
                }
                else
                {
                    //return timespan.ToString("m'm 's's'");
                    return timespan.ToString(formatIni["mm"]);//("mm'm 'ss's'");
                }
            }
            else if (totalHours < 24)
            {
                return timespan.ToString(formatIni["hh"]);//("hh'h'");
            }
            else
            {
                return timespan.ToString(formatIni["dd"]);//("d'd'");
            }
        }
    }
    /// <summary>
    /// 分秒格式化
    /// </summary>
    /// <param name="timespan"></param>
    /// <returns></returns>
    public static string FormatHHMM(TimeSpan timespan)
    {
        double totalHours = timespan.TotalHours;//这个带小数。。。。
        if (totalHours < 0)
        {
            return "";
        }
        else
        {
            NLIni formatIni = _FormatIni;
            string fs = "";
            if (totalHours < 1)
            {
                fs = formatIni["mm"].AsString;
            }
            else if (totalHours < 24)
            {
                fs = formatIni["hhmm"].AsString;
            }
            else if (totalHours >= 24)
            {
                fs = formatIni["ddhh"].AsString;
            }

            return timespan.ToString(fs);
        }
    }



    public static TimeSpan TimeZoneOffest
    {
        get
        {
            return DateTime.Now - DateTime.UtcNow;
        }
    }
    /// <summary>
    /// 将策划配表时间转换成服务器所在时区的时间戳（时间格式：2023-12-01 00:00:00）
    /// </summary>
    /// <param name="str">时间格式：2023-12-01 00:00:00</param>
    /// <returns></returns>
    public static long GetTimeStampByTimeString(string str)
    {

        DateTime dt = Convert.ToDateTime(str);
        return new DateTimeOffset(dt).ToUnixTimeSeconds();
    }

    public static bool IsSameDay(DateTime date1, DateTime date2)
    {
        if (date1.Year == date2.Year)
            if (date1.Month == date2.Month)
                return date1.Day == date2.Day;

        return false;
    }
}
